<template>
  <div class="setting">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first" class="first">
          <div>
            <el-button type="primary" size="mini" @click="openAdd"
              ><i class="el-icon-plus"></i>新增角色</el-button
            >
          </div>

          <div class="table">
            <el-table :data="list" border style="width: 100%">
              <el-table-column type="index" label="序号" width="160">
              </el-table-column>
              <el-table-column prop="name" label="角色名" width="160">
              </el-table-column>
              <el-table-column prop="description" label="描述">
              </el-table-column>
              <el-table-column label="操作" width="250">
                <template slot-scope="{ row }">
                  <el-button type="text" @click="openShow(row)"
                    >分配权限</el-button
                  >
                  <el-button type="text" @click="openEdit(row)">修改</el-button>
                  <el-button type="text" @click="delRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="pagination">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="pageSizes"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
            >
            </el-pagination>
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">公司信息</el-tab-pane>
      </el-tabs>
    </el-card>

    <el-dialog :title="title" :visible="showDialog" @close="cancle">
      <el-form :model="form" label-width="100px">
        <el-form-item label="角色名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="editRole">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分配角色弹框 -->
    <setper ref="setper" :show.sync="show" :username="username"></setper>
  </div>
</template>

<script>
import {
  getRoleApi,
  editRoleByIdApi,
  addRoleApi,
  delRoleByIdApi
} from '@/api/setting.js'
export default {
  name: 'setting',
  components: {
    setper: () => import('./components/setper.vue')
  },
  data () {
    return {
      activeName: 'first',
      list: [],
      currentPage: 1,
      total: 0,
      pageCount: 0,
      pageSizes: [2, 4, 6, 8],
      pagesize: 2,
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      title: '',
      show: false,
      username: ''
    }
  },
  methods: {
    // 当前页容量改变时触发
    handleSizeChange (size) {
      this.pagesize = size
      this.getRoleList()
    },
    // 选中页码改变时触发
    handleCurrentChange (page) {
      this.currentPage = page
      this.getRoleList()
    },
    // 获取所有角色
    async getRoleList () {
      const res = await getRoleApi({
        pagesize: this.pagesize,
        page: this.currentPage
      })

      this.list = res.data.rows
      this.total = res.data.total
    },
    // 打开编辑面板
    openEdit (row) {
      this.title = '编辑角色'
      this.showDialog = true
      this.form = { ...row }
    },
    // 关闭dialog框
    cancle () {
      this.showDialog = false
      this.form = {
        name: '',
        description: ''
      }
    },
    // 打开新增面板
    openAdd () {
      this.title = '新增角色'
      this.showDialog = true
    },
    // 编辑和新增功能实现
    async editRole () {
      try {
        if (this.form.id) {
          // 编辑
          await editRoleByIdApi(this.form)
        } else {
          // 新增
          await addRoleApi(this.form)
        }

        this.$message.success(this.form.id ? '修改成功' : '新增成功')

        this.getRoleList()
        this.showDialog = false
        this.form = {
          name: '',
          description: ''
        }
      } catch (error) {
        console.log('出错啦')
      }
    },
    // 删除角色实现
    async delRole (id) {
      try {
        await this.$confirm('请确认是否删除数据', '温馨提示')
        await delRoleByIdApi(id)
        this.$message.success('删除成功')
        if (this.list.length === 1) {
          this.currentPage--
        }
        this.getRoleList()
      } catch (error) {
        console.log('出错了')
      }
    },
    // 分配角色弹框
    openShow (row) {
      this.username = row.name
      this.$refs.setper.getAllPermission()
      this.$refs.setper.getRoleDetail(row.id)
      this.show = true
    }
  },
  created () {
    this.getRoleList()
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 20px;

  .first {
    padding: 40px 20px;

    .table {
      margin-top: 15px;
    }

    .pagination {
      margin-top: 30px;
    }
  }
}
</style>
